package com.ilongsay.service.impl;

import com.ilongsay.dao.SettingDao;
import com.ilongsay.entity.*;
import com.ilongsay.service.ISettingService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * @Author ilongsay
 * @Email ilongsay@163.com
 * @Copyright 版本所有，侵权必究！
 * @Datetime 2018-12-09 22:17
 * @Describution
 */
@Service("settingService")
public class SettingServiceImpl implements ISettingService {

    @Autowired
    private SettingDao settingDao;

    @Override
    public ChaiFenSetting findChaifenSetting() {
        return settingDao.findChaiSetting();
    }

    @Override
    public void updateChaifenSetting(ChaiFenSetting setting) {
        setting.getAmount().replace(" ", "");
        settingDao.updateChaiSetting(setting);
    }

    /**
     * @param
     * @return com.ilongsay.entity.AjaxObject
     * 系统设置代码
     */
    @Override
    public AjaxObject querySetting() {
        AjaxObject ajaxObject = new AjaxObject();
        Setting setting = null;
        try{
            setting = settingDao.querySetting();
        }catch( Exception e ){
            e.printStackTrace();
            ajaxObject.setCode("1");
            ajaxObject.setMsg("查询数据失败"+e.toString());
            return ajaxObject;
        }
        ajaxObject.setCode("0");
        ajaxObject.setData(setting);
        return ajaxObject;
    }

    /**
     * @param limitType
     * @return java.lang.Object
     * 更新限额的系统设置
     */
    @Override
    public Object updateSysSetting(LimitType limitType) {
        //这里其实可以不必要这样处理，为了日后的可维护性
        if (Integer.valueOf(limitType.getSelect())==0) {
            limitType.setLimit("10000");
        } else if (Integer.valueOf(limitType.getSelect())==1) {
            limitType.setLimit("100000");
        } else if (Integer.valueOf(limitType.getSelect())==2) {
            limitType.setLimit("1000000");
        } else if (Integer.valueOf(limitType.getSelect())==3) {
            limitType.setLimit("10000000");
        } else if (Integer.valueOf(limitType.getSelect())==4) {
            limitType.setLimit("100000000");
        }
        AjaxObject ajaxObject = new AjaxObject();
        try{
            settingDao.updateSetting(limitType);
        }catch( Exception e ){
            ajaxObject.setCode("1");
            ajaxObject.setMsg("更新系统设置失败！");
            e.printStackTrace();
            return ajaxObject;
        }
        ajaxObject.setCode("0");
        return ajaxObject;
    }

    /**
     * @param type 发票类型
     * @return java.lang.Object
     * 查询限额的设置
     */
    @Override
    public Object queryLimitSetting(String type) {
        AjaxObject ajaxObject = new AjaxObject();
        LimitType limitType = null;
        try{
            limitType = settingDao.queryLimitSetting(type);
        }catch( Exception e ){
            ajaxObject.setCode("1");
            ajaxObject.setMsg("查询数据失败！");
            e.printStackTrace();
            return ajaxObject;
        }
        ajaxObject.setCode("0");
        ajaxObject.setData(limitType);
        return ajaxObject;
    }

    /**
     * @param setting 设置
     * @return java.lang.Object
     * 保存系统设置
     */
    @Override
    public Object preserveSyeSetting(Setting setting) {
        AjaxObject ajaxObject = new AjaxObject();
        try{
            settingDao.updateSysSetting(setting);
            ajaxObject.setCode("0");
            ajaxObject.setMsg("系统设置保存成功！");
        }catch( Exception e ){
            ajaxObject.setCode("1");
            ajaxObject.setMsg("系统设置保存失败！");
            e.printStackTrace();
            return ajaxObject;
        }
        return ajaxObject;
    }

    @Override
    public Object queryErrorAdjustSetting() {
        AjaxObject ajaxObject = new AjaxObject();
        ErrorAdjust errorAdjust = settingDao.queryErrorAdjust();
        ajaxObject.setMsg("");
        ajaxObject.setCode("0");
        ajaxObject.setData(errorAdjust);
        return ajaxObject;
    }

    /**
     * @param errorAdjust 传入的参数
     * @return com.ilongsay.entity.AjaxObject
     * 更新误差调整的设置参数
     */
    @Override
    public AjaxObject updateErrorAdjustSetting(ErrorAdjust errorAdjust) {
        AjaxObject ajaxObject = new AjaxObject();

        if (errorAdjust.getAutoType().equals("0")) {
            try{
                Double.valueOf(errorAdjust.getAmount().replace(" ", ""));
            }catch( Exception e ){
                e.printStackTrace();
                ajaxObject.setCode("1");
                ajaxObject.setMsg("输入数据有误，请检查数据是否包含其他字符串！"+e.toString());
                return ajaxObject;
            }
        }
        settingDao.updateErrorAdjust(errorAdjust);
        String[] billCodes = errorAdjust.getFixType().split(",");

        for (int i = 0; i < billCodes.length; i++) {
            settingDao.updateFixError(billCodes[i], i+1);
        }
        if (billCodes.length == 1) {
            settingDao.updateFixError("3", 2);//如果传进来的数据是长度是1，那么就将金额的id为2的更新为3，不选中
        }
        ajaxObject.setCode("0");
        ajaxObject.setMsg("更新误差信息调整成功！");
        return ajaxObject;
    }
}
